package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.common.constant.Const;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwdhT;
import cn.e4j.modular.crm.entity.GwdheT;
import cn.e4j.modular.crm.entity.GwdhoT;
import cn.e4j.modular.crm.mapper.GwdhTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static cn.e4j.core.util.DateUtils.PATTERN_yyyy_MM_dd;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-03-14
 */
@Service
public class GwdhTServiceImpl extends ServiceImpl<GwdhTMapper, GwdhT> {

    @Autowired
    private SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        List<Map<String, Object>> paramsDtos = new ArrayList<>();
        if (user.getACCOUNT().equals("admin")) {
            paramsDtos = this.baseMapper.listPage(page, dto);
        } else {
            //客户权限
            dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
            //部门权限
            dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
            //渠道权限
            dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
            paramsDtos = this.baseMapper.listDataPage(page, dto);
        }
        for (int i = 0; i < paramsDtos.size(); i++) {
            String files = "";
            List<Dto> list = this.baseMapper.getFiles(paramsDtos.get(i).get("GWDHDOCNO").toString());
            for (int j = 0; j < list.size(); j++) {
                if (j == 0) {
                    files = list.get(j).getString("GWDHE002");
                } else {
                    files = files + ',' + list.get(j).getString("GWDHE002");
                }
            }
            paramsDtos.get(i).put("GWDHE", files);
        }
        return paramsDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getUsersList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getUsersList(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getOrgsList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getOrgsList(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwakList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getGwakList(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwaeList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getGwaeList(page, dto);
        return listData;
    }

    @Transactional
    public Dto addData(List<GwdhT> gwdhTList, List<GwdhoT> gwdhoTList, List<GwdheT> gwdheTList) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser user = ShiroKit.getUser();
        GwdhT gwdh = gwdhTList.get(0);
        //增加新的明细
        Dto dto = this.baseMapper.getGwdh(gwdh.getGWDHDOCNO());
        if (dto != null) {
            outDto.setAppCode(400);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return outDto;
        } else {
            gwdh.setGWDHENT(user.getDBC());
            gwdh.insert();
            for (GwdhoT gwdho : gwdhoTList) {
                gwdho.setGWDHODOCNO(gwdh.getGWDHDOCNO());
                gwdho.setGWDHOENT(user.getDBC());
                gwdho.insert();
            }
            int count = 1;
            for (GwdheT gwdhe : gwdheTList) {
                gwdhe.setGWDHEDOCNO(gwdh.getGWDHDOCNO());
                gwdhe.setGWDHEENT(user.getDBC());
                gwdhe.setGWDHE001(new BigDecimal(count));
                gwdhe.insert();
                count++;
            }
        }
        return outDto;
    }


    @Transactional
    public Dto editData(List<GwdhT> gwdhTList, List<GwdhoT> gwdhoTList, List<GwdheT> gwdheTList) {
        ShiroUser user = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        GwdhT gwdh = gwdhTList.get(0);
        UpdateWrapper<GwdhT> gwdhTUpdateWrapper = new UpdateWrapper<>();
        gwdhTUpdateWrapper.eq("GWDHENT", gwdh.getGWDHENT());
        gwdhTUpdateWrapper.eq("GWDHDOCNO", gwdh.getGWDHDOCNO());
        gwdh.setGWDHMODU(user.getID());
        gwdh.setGWDHDATE(LocalDate.now());
        gwdh.setGWDHTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwdh.update(gwdhTUpdateWrapper);
        GwdhoT gwdho = new GwdhoT();
        UpdateWrapper<GwdhoT> gwdhoTUpdateWrapper = new UpdateWrapper<>();
        gwdhoTUpdateWrapper.eq("GWDHOENT", gwdh.getGWDHENT());
        gwdhoTUpdateWrapper.eq("GWDHODOCNO", gwdh.getGWDHDOCNO());
        gwdho.delete(gwdhoTUpdateWrapper);
        for (GwdhoT gwdhoT : gwdhoTList) {
            gwdhoT.setGWDHODOCNO(gwdh.getGWDHDOCNO());
            gwdhoT.setGWDHOENT(user.getDBC());
            gwdhoT.insert();
        }
        int count = 1;
        GwdheT gwdhe = new GwdheT();
        UpdateWrapper<GwdheT> gwdheTUpdateWrapper = new UpdateWrapper<>();
        gwdheTUpdateWrapper.eq("GWDHEENT", gwdh.getGWDHENT());
        gwdheTUpdateWrapper.eq("GWDHEDOCNO", gwdh.getGWDHDOCNO());
        gwdhe.delete(gwdheTUpdateWrapper);
        for (GwdheT gwdheT : gwdheTList) {
            gwdheT.setGWDHEDOCNO(gwdh.getGWDHDOCNO());
            gwdheT.setGWDHEENT(user.getDBC());
            gwdheT.setGWDHE001(new BigDecimal(count));
            gwdheT.insert();
            count++;
        }
        return outDto;
    }

    @Transactional
    public void delete(GwdhT gwdh) {
        UpdateWrapper<GwdhT> gwdhTUpdateWrapper = new UpdateWrapper<>();
        gwdhTUpdateWrapper.eq("GWDHENT", gwdh.getGWDHENT());
        gwdhTUpdateWrapper.eq("GWDHDOCNO", gwdh.getGWDHDOCNO());
        gwdh.delete(gwdhTUpdateWrapper);

        GwdhoT gwdho = new GwdhoT();
        Dto dto = this.baseMapper.getGwdho(gwdh.getGWDHDOCNO());
        if (dto != null) {
            UpdateWrapper<GwdhoT> gwdhoTUpdateWrapper = new UpdateWrapper<>();
            gwdhoTUpdateWrapper.eq("GWDHOENT", gwdh.getGWDHENT());
            gwdhoTUpdateWrapper.eq("GWDHODOCNO", gwdh.getGWDHDOCNO());
            gwdho.delete(gwdhoTUpdateWrapper);
        }

        GwdheT gwdhe = new GwdheT();
        Dto inDto = this.baseMapper.getGwdhe(gwdh.getGWDHDOCNO());
        if (inDto != null) {
            UpdateWrapper<GwdheT> gwdheTUpdateWrapper = new UpdateWrapper<>();
            gwdheTUpdateWrapper.eq("GWDHEENT", gwdh.getGWDHENT());
            gwdheTUpdateWrapper.eq("GWDHEDOCNO", gwdh.getGWDHDOCNO());
            gwdhe.delete(gwdheTUpdateWrapper);
        }
    }

    @Transactional
    public void delDataBatch(List<GwdhT> gwdhTList) {
        for (GwdhT gwdh : gwdhTList) {
            UpdateWrapper<GwdhT> gwdhTUpdateWrapper = new UpdateWrapper<>();
            gwdhTUpdateWrapper.eq("GWDHENT", gwdh.getGWDHENT());
            gwdhTUpdateWrapper.eq("GWDHDOCNO", gwdh.getGWDHDOCNO());
            gwdh.delete(gwdhTUpdateWrapper);

            GwdhoT gwdho = new GwdhoT();
            Dto dto = this.baseMapper.getGwdho(gwdh.getGWDHDOCNO());
            if (dto != null) {
                UpdateWrapper<GwdhoT> gwdhoTUpdateWrapper = new UpdateWrapper<>();
                gwdhoTUpdateWrapper.eq("GWDHOENT", gwdh.getGWDHENT());
                gwdhoTUpdateWrapper.eq("GWDHODOCNO", gwdh.getGWDHDOCNO());
                gwdho.delete(gwdhoTUpdateWrapper);
            }

            GwdheT gwdhe = new GwdheT();
            Dto inDto = this.baseMapper.getGwdhe(gwdh.getGWDHDOCNO());
            if (inDto != null) {
                UpdateWrapper<GwdheT> gwdheTUpdateWrapper = new UpdateWrapper<>();
                gwdheTUpdateWrapper.eq("GWDHEENT", gwdh.getGWDHENT());
                gwdheTUpdateWrapper.eq("GWDHEDOCNO", gwdh.getGWDHDOCNO());
                gwdhe.delete(gwdheTUpdateWrapper);
            }
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwdhoData(String GWDHODOCNO) {
        List<Dto> listData = this.baseMapper.getGwdhoData(GWDHODOCNO);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getDetailData(String GWDHDOCNO) {
        Dto dto = this.baseMapper.getDetailData(GWDHDOCNO);
        List<Dto> listData = this.baseMapper.getFiles(GWDHDOCNO);
        if (listData.size() > 0) {
            dto.put("file", listData);
        }
        return dto;
    }

    @Transactional(readOnly = true)
    public List<Dto> getNotices(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        List<Dto> listData = new ArrayList<>();
        dto.put("GWDHENT", "99");
        if (user.getID().equals(Const.ADMIN_ID+"")) {
            listData = this.baseMapper.getNoticesByAdmin(dto);
        } else {
            if (dto.getString("GWDH002").equals("") || dto.getString("GWDH002").equals(null)) {
                return listData;
            } else {
                dto.put("GWDH006", DateUtils.formatDate(new Date(), PATTERN_yyyy_MM_dd));
            }
            if (dto.getString("GWDH002").equals("1")) {
                //部门权限
                dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
            }
            if (dto.getString("GWDH002").equals("2")) {
                //渠道权限
                dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
            }
            if (dto.getString("GWDH002").equals("3")) {
                //客户权限
                dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
            }

            listData = this.baseMapper.getNotices(dto);
        }
        return listData;
    }
}
